﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using MM.DBUtility;
using MM.Common;
using System.Collections;

namespace WebMMS.Base
{
    public partial class TimersAdd : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                if (Session["CurUser"] == null)
                {
                    Response.Redirect("~/Login.aspx");
                    return;
                }
                if (!IsPostBack)
                {
                    BincblPID(ref cblPID);
                    BindcblAppID(ref cblAPPID);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        #region UserControl Bind
        private void BincblPID(ref CheckBoxList cbl)
        {
            cbl.Items.Clear();
            cbl.DataSource = DbHelperSql.Query("SELECT PID,PID+'-'+ PValue as Name FROM T_Pinfo WITH(NOLOCK) ").Tables[0];
            cbl.DataValueField = "PID";
            cbl.DataTextField = "Name";
            cbl.DataBind();
        }
        private void BindcblAppID(ref CheckBoxList cbl)
        {
            cbl.DataSource = DbHelperSql.Query("SELECT APPID,APPID+'-'+ APPValue as Name FROM T_APPinfo WITH(NOLOCK) ").Tables[0];
            cbl.DataValueField = "APPID";
            cbl.DataTextField = "Name";
            cbl.DataBind();
        }
        #endregion

        protected void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                string strPIDs = string.Empty;
                for (int i = 0; i < cblPID.Items.Count; i++)
                {
                    if (cblPID.Items[i].Selected)
                        strPIDs += cblPID.Items[i].Value + ",";
                }
                if (string.IsNullOrEmpty(strPIDs))
                {
                    MM.Common.Common.PageStartAlert(this.Page, "必须选择PID！");
                    return;
                }

                string strAPPIDs = string.Empty;
                for (int i = 0; i < cblAPPID.Items.Count; i++)
                {
                    if (cblAPPID.Items[i].Selected)
                        strAPPIDs += cblAPPID.Items[i].Value + ",";
                }
                if (string.IsNullOrEmpty(strAPPIDs))
                {
                    MM.Common.Common.PageStartAlert(this.Page, "必须选择APPID！");
                    return;
                }

                if (string.IsNullOrEmpty(txtTimer.Text.Trim()))
                {
                    MM.Common.Common.PageStartAlert(this.Page, "联网时间间隔不能为空！");
                    return;
                }

                string[] argPID = strPIDs.Remove(strPIDs.LastIndexOf(",")).Split(',');
                string[] argAPPID = strAPPIDs.Remove(strAPPIDs.LastIndexOf(",")).Split(',');

                ArrayList alSQL = new ArrayList();
                for (int i = 0; i < argPID.Length; i++)
                {
                    for (int j = 0; j < argAPPID.Length; j++)
                    {
                        if (MM.DAL.RuleInfo.ExistTimer(argPID[i], argAPPID[j]))
                        {
                            alSQL.Add(string.Format("DELETE FROM T_Timers WHERE PID='{0}' AND APPID='{1}' ", argPID[i], argAPPID[j]));
                        }
                        alSQL.Add(string.Format("INSERT INTO T_Timers(Pid,Appid,Timer) values('{0}','{1}',{2})", argPID[i], argAPPID[j], txtTimer.Text.Trim()));
                    }
                }
                MM.DBUtility.DbHelperSql.ExecuteSqlTran(alSQL);
                MM.Common.Common.PageAlertAndRedirect(this.Page, "添加成功！", "Timers.aspx");
            }
            catch (Exception ex)
            {

                throw ex;
            }
        }
    }
}